package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;
import dao.UserMod;

/**
 * Servlet implementation class XuLyDangNhap
 */
@WebServlet("/XuLyDangNhap")
public class XuLyDangNhap extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public XuLyDangNhap() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String user = request.getParameter("user");
		String password = request.getParameter("password");
		HttpSession session = request.getSession();
		String mainPage = "";
		User checkName = null;
		User users;
		String but1 = "", but2 = "";
		String cm = "";
		String url = "/login.jsp";
		checkName = UserMod.getIsntance().checkLogin(user, password);
		System.out.println(user + password);
		
		if (checkName == null) {
			cm += " User or password failue";
			request.setAttribute("cm", cm);
			
		} else {
			
			int level = checkName.getLevel();
			if (level == 1)
				url = "/giaovu.jsp";
			else if (level == 2)
				url = "/giaovien.jsp";
			else
				url = "/phuhuynh.jsp";
			

		}
		but1 += "dang xuat";
		but2 += checkName.getUser();
		request.setAttribute("but1", but1);
		request.setAttribute("but2", but2);
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
		// {
		// but1 += "Logout";
		// but2 += "Me";
		// request.setAttribute("but1", but1 );
		// request.setAttribute("but2", but2);
		// mainPage = "/HomePage.jsp";
		// RequestDispatcher dispatcher = getServletContext()
		// .getRequestDispatcher(mainPage);
		// dispatcher.forward(request, response);
		// }
	}
	

}
